package com.wuyiz.learning.Net.TCPThreadPool;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @ClassName FileUpLoadThreadPool
 * @Description: TODO
 * @Author AllureLove
 * @Date 2019/11/15 
 * @Version V1.0
 **/
public class UpLoadClientRunnable implements Runnable {
    private String host;
    private int port;
    private String fileName;

    public UpLoadClientRunnable(String host, int port, String fileName) {
        this.host = host;
        this.port = port;
        this.fileName = fileName;
    }

    @Override
    public void run() {
        try {
            //1.创建本地字节输入流FileInputStream对象，绑定数据源
            FileInputStream fileInputStream = new FileInputStream(fileName);
            //2.创建客户端Socket对象，绑定IP地址和端口
            Socket socket = new Socket(host, port);

            //3.使用Socket中的getOutputStream()获取网络字节输出流OutputStream对象
            OutputStream outputStream = socket.getOutputStream();
            //4.创建本地字节输入流FileInputStream对象中的read()，读取本地文件
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = fileInputStream.read(bytes)) != -1) {
                //5.使用网络字节输出流OutputStream对象中的write()，把读取到的文件上传到服务器
                outputStream.write(bytes, 0, len);
            }

            /**
             * 由于服务器端不能读到-1，程序会进入阻塞状态，需要给服务器添加结束标记
             * void shutdownOutput​() 禁用此套接字的输出流
             *      对于TCP套接字，任何先前写入的数据将被发送，随后是TCP的正常连接终止序列
             */
            socket.shutdownOutput();

            //6.使用Socket中的getInputStream()获取网络字节输出流InputStream对象
            InputStream inputStream = socket.getInputStream();
            //7.使用网络字节输入流InputStream对象中的read()读取服务回写的数据
            while ((len = inputStream.read(bytes)) != -1) {
                System.out.println(new String(bytes, 0, len));
            }
            //8.释放资源(FileInputStream, Socket)
            fileInputStream.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
